<?php include('../../variables/variables.php'); ?>

<?php

if(!session_id()) session_start(); 
$con = mysql_connect($dbserver, $dbuser, $dbpass);
if ($con)
{
	mysql_select_db($db, $con);
	
	if ($_FILES["cover_image"]['name'] && $_FILES["cover_image"]["error"] > 0)
	{
		header("location: /manage_book.php?err=IMG upload error: ". $_FILES["cover_image"]["error"]);
		exit;
	}

	if ($_POST['title'] && $_POST['isbn'] && $_POST['authors'] && $_POST['no_of_pages'])
	{
		$cover_image = $_POST['prev_cover_img'];
		if ($_FILES["cover_image"]['name'])
		{
			$cover_image = $cover_image_location . $_POST['isbn'] . "." . end(explode('.', $_FILES["cover_image"]["name"]));
		}
	
		$q = fquery($update_book, array( $_POST['title'], $_POST['edition'], $_POST['isbn'], $_POST['authors'], $_POST['year'], $_POST['publisher'], $_POST['no_of_pages'], $_POST['description'], $_POST['location'], $_POST['format'], $cover_image , $_POST['dewey_decimal_number'], $_POST['mediaID']));
		//echo $q;
		//exit;
		
		$result = mysql_query($q);
		mysql_close($con);
		if ($result)
		{
			if ($_FILES["cover_image"]['name'])
			{
				if (file_exists("../../".$cover_image))
				{
					unlink("../../".$cover_image);
				}

				move_uploaded_file($_FILES["cover_image"]["tmp_name"], "../../".$cover_image);
				
			}
			header("location: /manage_book.php?msg=Book Updated successfully!");
		}
		else
		{
			header("location: /manage_book.php?err=Failed to update the book!");// <br> ". $q);
		}
	}
	else
	{
		mysql_close($con);
		header("location: /manage_book.php?err=Please fill in all fields.");
	}
}

?>
